<?
  class Input
  {
	var $cValue;
	var $bMandatory;
	var $cLabel;
	var $cUnits;
	var $cPrefixLabel;
	var $cAccessKey;
	var $cField;
	var $bHidden = false;
	var $bDisabled;
	var $aEvents = array();
	var $cHelp = '';
	var $oForm;
	var $cFormName;
	var $aActions;
	var $cWidth;
	var $cStyle;
	var $cStyleClass;
	var $cEmptyText;

	function setEmptyText( $cEmptyText )
		{
			$this->cEmptyText = $cEmptyText;
		}

	function getValueAsArray ( $cSeparator = ',' )
		{
			return ( is_array($this->cValue) )  ? $this->cValue : explode( $cSeparator , $this->cValue);
		}
	function getValueAsString ()
		{
			return $this->getValue ();
		}
	function setValueFromString ( $cValue )
		{
			$this->setValue ( $cValue );
		}
	function getField()
	{
		return $this->cField;
	}
	function setField( $cField)
	{
		$this->cField = $cField;
	}
	function setWidth( $cWidth )
	{
		$this->cWidth = $cWidth;
	}
	function make()
		{
			  if ( $this->bHidden )
			  {
				$cEventos = $this->getEvents();
				return "<input type=\"hidden\"  $cEventos id=\"" . $this->cField . "\"  name=\"" . $this->cField . "\" value=\"" . $this->cValue . "\" />";
			  }
		}
  function setDefault( $cValue )
	{
	  if ( $this->cValue == '' ) $this->setValue ( $cValue );
	}

  function setForm( &$oForm )
	{
	  $this->oForm =& $oForm;
	}
  function setFormName( $cFormName )
	{
		$this->cFormName = $cFormName;
	}
  function getFormName()
	{
		// Si esta seteado el valor => lo devuelve
		if ( $this->cFormName ) return $this->cFormName ;
		// Sino se fija si tiene el form
		if ( $this->oForm ) return $this->cFormName  = $this->oForm->cFormName;
		//  Y por ultimo se fija si viene por Parametro 
		$cFormName = getParam( 'cFormName' );
		if ( $cFormName ) 
		{
			$this->cFormName = $cFormName; 
			return $this->cFormName;
		}
		addMessage('No esta seteado el Form');
		return false;
	}

	// Precondiciones:
	// 		1. Tiene que estar seteado el Form o FormName
  function setOnChangeAutoSubmit( &$oForm )
	{
		if ( $oForm ) $this->setForm($oForm);
		if ( !$this->oForm) addMessage('No esta seteado el Form');
	  if ( MODO_AJAX == true )
	  {
		$cUrl = $this->oForm->getUrlAutoRefresh();
		$this->setOnChange( " return sendForm(\"$cUrl\", \"" . $this->getFormName() . "\",refreshElement, \"" . strtolower(get_class($this->oForm)) . "\", true, false  );"  );
	  } else
	  {
		$this->setOnChange( $this->getFormName() . ".action=\"#\";" . $this->getFormName() . ".submit();"  );
	  }
	}
  function setOnChangeSubmit( $bOn )
	{
	  $this->setOnChange( $this->getFormName() .  '.submit();' );
	}

  function setHelp ( $cHelp )
	{
		$this->cHelp = $cHelp;
		$this->aEvents['onfocus'] .= "showContextHelp(this);";
		$this->aEvents['onblur'] .= "hideContextHelp(this);";
	}
	function getHelp()
	{
		return $this->cHelp;
	}
	function showHelp()
	{
	  //$cUrl = $this->cHelpWindow . "&amp;" . $this->cHelpParam . '&amp;' . PARAM_MODE . '=' . POPUP;
	  //return "<a href='$cUrl' onclick=\"javascript: openWindow( '$cUrl', 'Ayuda Contextual' );return false;\" ><img alt='?' align='top' src='" . URL_IMAGES . "/ayuda.gif' /></a>";
	}
	function addAction ( $nModo, $cAction , $cIcon = '', $cNombre = '')
	{
	  if ( $cAction )
	  {
		$this->aActions[] = array ( $cAction, $cIcon , $cNombre, $nModo);
	  }
	}

	// PreCondiciones: Necesita tener seteado el FormName
	// $nModo:
	//			2. No pasa valor , aunque tenga no lo manda (ej el Alta )
	//			3. Se fija que tenga un dato el Input, y le pasa el valor ( ej Modificacion)
	// $cParametros: Son parametros fijos que se los agrega a la accion
	// $cField: Es optativo, se ingresa el nombre de un campo del formulario, y antes de abrir la accion, le pasa el valor que tiene.
  function addActionArray ( $nModo, $aAction , $cParametros = '', $cField = '')
	{
	  if ( is_array($aAction) )
	  {
		$aAction[0] .= $cParametros;
		$aAction[] = $nModo;
		$aAction[] = $cField;
		$this->aActions[] = $aAction;
	  }
	}
	// PC setFormName( $cFormName )
  function showActions()
	{
	  $cReturn = '';
	  if ( $this->bHidden || !is_array($this->aActions) ) return "";

	  foreach ( $this->aActions as $aAction )
	  {
		$nModo    = $aAction[3];
		$cIcono    = $aAction[1];
		$cNombre  = $aAction[2];
		$cField  = $aAction[4];
        $cUrl     = replaceString( PARAM_REFRESH, 'action', $aAction[0] );
        $cUrl     = replaceString( PARAM_MODE, DIV_POPUP, $cUrl );
		$cUrl = replaceString( PARAM_TARGET, "div_popup" . (getPopupNumber() + 1) . "_inside", $cUrl);
		$cHRef = "href='javascript:void(0);' onclick=\"javascript: openInput( '$cUrl', '" . $this->cField . "', '" . $this->getFormName() . "', '" . $this->cField . "' , $nModo, '$cField' ); return false; \" ";
		$cReturn .=  "&nbsp;<a $cHRef ><img src='$cIcono' witdh='16' height='16' style='border: none;' align='top' alt='$cNombre' title='$cNombre' /></a>";
	  }
	  return $cReturn;
	}
  function Input ($cLabel,  $cField, $cValue = '', $bMandatory = false, $bHidden= false)
	{
	  $this->cField     = $cField;
	  $this->cLabel     = $cLabel ;
	  $this->bMandatory = $bMandatory;
	  $this->bDisabled  = FALSE;
	  $this->bHidden    = $bHidden;
	  $this->cValue = FALSE;
	  if ( $cValue != '')
	  {
		$this->setValue( $cValue );
	  } else
	  {
		// Si no recibe el Valor => lo busca en los parametros de la pagina
		if ( getParam($cField) )
		{
		  $this->setValue ( getParam($cField) );
		}
	  }
	}
  function addStyle( $cStyle )
	{
		$this->cStyle .= $cStyle;
	}
  function setStyle( $cStyle )
	{
		$this->cStyle = $cStyle;
	}
  function setStyleClass( $cStyleClass )
	{
		$this->cStyleClass = $cStyleClass;
	}
  function getStyleHTML( )
	{
		$cReturn = '';
		if ( $this->cStyleClass ) $cReturn = " class='". $this->cStyleClass ."' ";
		elseif ( $this->bMandatory  ) $cReturn = " class='required' ";
		if ( $this->cStyle  ) $cReturn .= " style='" . $this->cStyle . "'  ";
		return $cReturn;
	}
  function getProperties()
	{
	  $cReturn = "";
	  if ( $this->bDisabled )
	  {
		$cReturn .= " disabled ";
	  }
	  if ( $this->cWidth || $this->cStyle )
	  {
		$cReturn .= " style='";
		if( $this->cWidth ) $cReturn .= " width: " . $this->cWidth . "px; ";
		if( $this->cStyle ) $cReturn .=  $this->cStyle ;
		$cReturn .= "'";
	  }
	  if ( $this->cHelp  ) $cReturn .= " title='" . $this->cHelp . "' ";
	  return $cReturn;
	}
  function getEvents()
  {
	$cReturn = "";
	foreach( $this->aEvents as $cEventName => $cEventFunction )
	{
		$cReturn .= " $cEventName='javascript: $cEventFunction;' ";
	}
	return $cReturn;
  }

  function setOnClick( $cEvent )
	{
	  $this->aEvents['onclick'] = $cEvent;
	}
  function setOnChange ( $cEvent )
	{
	  $this->aEvents['onchange'] = $cEvent;
	}
  function setOnKeyPress ( $cEvent )
	{
	  $this->aEvents['onkeypress'] = $cEvent;
	}
  function setOnBlur ( $cEvent, $bAppend = false )
	{
		if ( $bAppend )
			$this->aEvents['onblur'] .= $cEvent;
		else
			$this->aEvents['onblur'] = $cEvent;
	}
  function setOnKeyUp( $cEvent )
	{
	  $this->aEvents['onkeyup'] = $cEvent;
	}
  function setOnFocus( $cEvent )
	{
	  $this->aEvents['onfocus'] = $cEvent;
	}
  function setOnKeyDown( $cEvent )
	{
	  $this->aEvents['onkeydown'] = $cEvent;
	}
  function setDisabled ( $bOn = true )
	{
	  $this->bDisabled = $bOn;
	}
  function isEmpty ()
	{
	  return (empty($this->cValue) || $this->cValue == $this->cEmptyText) ? true: false;
	}
  function setHidden ( $bOn = true )
	{
	  $this->bHidden = $bOn;
	}
  function setIdLabel ( $cPrefixLabel = 'lbl' )
	{
	  $this->cPrefixLabel = $cPrefixLabel;
	}
  function isHidden()
	{
	  return $this->bHidden;
	}
  function getLabel()
	{
	  return $this->cLabel;
	}
  function setLabel ( $cLabel , $cAccessKey = '' )
	{
	  $this->cLabel = $cLabel;
	  $this->cAccessKey = $cAccessKey;
	}
  function getUnits()
	{
	  return $this->cUnits;
	}
  function setUnits( $cUnits )
	{
	  $this->cUnits = $cUnits;
	}
  function showLabel( $cLabel = '', $cAccessKey = '' )
	{
	  // Si esta oculto => no muestra nada
	  if ( $this->bHidden ) return "";
	  // Si Recibe la descripcion => actualiza la propiedad
	  if ( $cLabel != '' )
	  {
		$this->setLabel ( $cLabel, $cAccessKey );
	  }
	  // Si no tiene Descripcion => no muestra nada
	  if ( $this->cLabel == '' ) return false;
	  // Subraya el AccessKey en la Descripcion
	  if ( $this->cAccessKey )
	  {
		$nPos = strpos( $this->cLabel, $this->cAccessKey );
	  }
	  if ( $this->cPrefixLabel ) $cPropiedades = " id='" . $this->cPrefixLabel .  $this->cField  . "' ";
	  if ( $this->cHelp ) $cPropiedades .= " title='" . $this->cHelp . "'"; // onmouseover='showContextHelp(this);' onmouseout='hideContextHelp(this);' ";
	  // Si no tiene accesskey o no lo encontro en la descripcion
	  if ( $nPos === false || $this->cAccessKey == '' )
	  {
		$cReturn = "<label $cPropiedades  for='" . $this->cField . "' >" .$this->cLabel . " </label>";
	  } else
	  {
		$cLabel = substr( $this->cLabel, 0, $nPos ) . '<u>' . substr( $this->cLabel, $nPos, strlen($this->cAccessKey) ) . '</u>' . substr( $this->cLabel, $nPos+ strlen($this->cAccessKey) ) ;
		//if ( $this->bMandatory ) $cLabel  = "<b>$cLabel</b>";
		$cReturn = "<label $cPropiedades for='" . $this->cField . "' accesskey='" . $this->cAccessKey . "'>$cLabel </label>";
	  }
	  return $cReturn;
	}
	
  function unserializeValue( $cSeparator = ',' )
	{
		if ( is_array( $this->cValue ) )
			$this->cValue = implode($cSeparator, $this->cValue );
	}
  function setValue( $cValue )
	{
	  $this->cValue = $cValue;
	}
  function getValue()
	{
	  return $this->cValue == $this->cEmptyText ? '': $this->cValue;
	}
  function setMandatory( $bMandatory = true )
	{
	  $this->bMandatory = $bMandatory;
	}
  }
?>
